package com.etc.traval.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.mail.Multipart;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Max;

import com.etc.traval.biz.PlaceBiz;
import com.etc.traval.biz.PlacecomBiz;
import com.etc.traval.bizimpl.PlaceBizImpl;
import com.etc.traval.bizimpl.PlacecomBizImpl;
import com.etc.traval.util.FileUp;
import com.etc.traval.util.RenamePolicyCos;
import com.etc.travel.entity.Place;
import com.etc.travel.entity.Placecom;
import com.oreilly.servlet.MultipartRequest;

/**
 * Servlet implementation class PlaceServlet
 */
@WebServlet("/PlaceServlet")
public class PlaceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PlaceServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		//获取传入的flag值
		PlaceBiz biz=new PlaceBizImpl();
		String flag=null;
		if(request.getParameter("flag")!=null){
			flag = request.getParameter("flag");
		}
		if(flag.equals("place")){
			List<Place> list1=biz.queryAllByNameKey("东北");
			List<Place> list2=biz.queryAllByNameKey("华北");
			List<Place> list3=biz.queryAllByNameKey("华中");
			List<Place> list4=biz.queryAllByNameKey("华东");
			List<Place> list5=biz.queryAllByNameKey("华南");
			List<Place> list6=biz.queryAllByNameKey("西南");
			List<Place> list7=biz.queryAllByNameKey("西北");
				request.setAttribute("list1", list1);
				request.setAttribute("list2", list2);
				request.setAttribute("list3", list3);
				request.setAttribute("list4", list4);
				request.setAttribute("list5", list5);
				request.setAttribute("list6", list6);
				request.setAttribute("list7", list7);
			request.getRequestDispatcher("place.jsp").forward(request, response);
			out.flush();
			out.close();
		}else if(flag.equals("add")){
			String savepath = request.getRealPath("/upload");
			int maxsize = 100 * 1024 * 1024;
			//构造一个MultipartRequest对象，依次传入内置对象request,保存路径，上传的最大长度，编码格式
			MultipartRequest req = new MultipartRequest(request, savepath, maxsize, "utf-8", new RenamePolicyCos());
			String name1 = req.getParameter("name1");
			String name2 = req.getParameter("name2");
			String name = name2+"·"+name1;
			String content = req.getParameter("content");
			String files=null;
			try {
				files = FileUp.uploadfiles(req);
			} catch (Exception e) {
				e.printStackTrace();
			}
			Place place=new Place();
			place.setPlace_content(content);
			place.setPlace_name(name);
			place.setPlace_logo(files);
			List<Place> list=biz.queryAllPlace(1, Integer.MAX_VALUE);
			for (Place place2 : list) {
				if(place2.getPlace_name().equals(name)){
					out.print("<script>alert('该目的地已存在	'); location.href='addplace.jsp';</script>");
					out.flush();
					out.close();
					return;
				}
			}
			if(biz.addPlace(place)){
				request.setAttribute("place", place);
				out.print("<script>alert('添加成功')</script>");
				request.getRequestDispatcher("PlaceServlet?flag=place").forward(request, response);
			}else{
				out.print("<script>alert('添加失败！！！'); location.href='addplace.jsp';</script>");
			}
			out.flush();
			out.close();
		}else if(flag.equals("query")){
			int op=Integer.valueOf(request.getParameter("op"));
			Place place=biz.queryPlaceById(op);
			PlacecomBiz biz1=new PlacecomBizImpl();
			List<Placecom> list=biz1.queryPlacecomByPlace(place, 1, 5);
			int count=biz1.queryCountByPlace(place);
			int pageCount = (count % 5 == 0) ? (count / 5) : (count / 5 + 1);
			if(place!=null){
				int nextindex=2;
				int pageIndex=1;
				request.setAttribute("count", count);
				request.setAttribute("list", list);
				request.setAttribute("place", place);
				request.setAttribute("pageCount", pageCount);
				request.setAttribute("nextindex", nextindex);
				request.setAttribute("pageIndex", pageIndex);
				request.getRequestDispatcher("yesplace.jsp").forward(request, response);
			}else{
				out.print("<script>alert('跳转失败！！！'); location.href='PlaceServlet?flag=place';</script>");
			}
		}else if(flag.equals("queryplace")){
			String key=request.getParameter("key");
			List<Place> list=biz.queryAllByNameKey(key);
				request.setAttribute("list", list);
				List<Place> list1=biz.queryAllByNameKey("东北");
				List<Place> list2=biz.queryAllByNameKey("华北");
				List<Place> list3=biz.queryAllByNameKey("华中");
				List<Place> list4=biz.queryAllByNameKey("华东");
				List<Place> list5=biz.queryAllByNameKey("华南");
				List<Place> list6=biz.queryAllByNameKey("西南");
				List<Place> list7=biz.queryAllByNameKey("西北");
				String name=null;
				if(list.size()==0){
					name="亲！没找到哦！";
				}else{
					name="根据“"+key+"”查询到的景点：";
				}
					request.setAttribute("key", name);
					request.setAttribute("list1", list1);
					request.setAttribute("list2", list2);
					request.setAttribute("list3", list3);
					request.setAttribute("list4", list4);
					request.setAttribute("list5", list5);
					request.setAttribute("list6", list6);
					request.setAttribute("list7", list7);
			request.getRequestDispatcher("place.jsp").forward(request, response);
			out.flush();
			out.close();
		}
	}
}
